package sipus.action.master.helper;

import org.hibernate.Session;
import org.zkoss.zk.ui.Sessions;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zul.Borderlayout;
import org.zkoss.zul.Center;
import org.zkoss.zul.Column;
import org.zkoss.zul.Columns;
import org.zkoss.zul.Grid;
import org.zkoss.zul.Label;
import org.zkoss.zul.Messagebox;
import org.zkoss.zul.Row;
import org.zkoss.zul.Rows;
import org.zkoss.zul.South;
import org.zkoss.zul.Textbox;
import org.zkoss.zul.Toolbar;
import org.zkoss.zul.Toolbarbutton;
import org.zkoss.zul.Window;

import sipus.common.Common;
import sipus.database.hibernate.HibernateUtil;
import sipus.database.model.Tbmuser;


public class ChangePasswordWindow extends Window {

	/**
	 * 
	 */
	private static final long serialVersionUID = -2363480108987559148L;

	private Textbox passwordLama;
	private Textbox passwordBaru;
	private Textbox passwordBaruRepeat;

	public ChangePasswordWindow() {
		super();

		try {

			init();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public ChangePasswordWindow(String title, String border, boolean closable)
			throws Exception {
		super(title, border, closable);
		init();
	}

	private void init() {

		setClosable(true);
		setTitle("Change Password");
		setWidth("500px");
		setHeight("200px");
		setPosition("center");

		Borderlayout borderlayout = new Borderlayout();
		borderlayout.setParent(this);
		Center center = new Center();
		center.setParent(borderlayout);
		center.setFlex(true);

		Grid grid = new Grid();
		grid.setParent(center);
		grid.setWidth("100%");
		grid.setHeight("100%");
		// grid.setOddRowSclass("non-odd");

		Columns columns = new Columns();
		columns.setParent(grid);
		Column column = new Column();
		column.setWidth("30%");
		column.setParent(columns);
		column = new Column();
		column.setWidth("70%");
		column.setParent(columns);

		Rows rows = new Rows();
		rows.setParent(grid);

		Row row = new Row();
		row.setStyle("border:0px;background: transparent;");
		row.setParent(rows);
		row.appendChild(new Label("Password Lama"));
		row.appendChild(passwordLama = new Textbox());
		passwordLama.setType("password");
		passwordLama.setCols(40);

		row = new Row();
		row.setStyle("border:0px;background: transparent;");
		row.setParent(rows);
		row.appendChild(new Label("Password Baru"));
		row.appendChild(passwordBaru = new Textbox());
		passwordBaru.setType("password");
		passwordBaru.setCols(40);

		row = new Row();
		row.setStyle("border:0px;background: transparent;");
		row.setParent(rows);
		row.appendChild(new Label("Ulangi Password Baru"));
		row.appendChild(passwordBaruRepeat = new Textbox());
		passwordBaruRepeat.setCols(40);
		passwordBaruRepeat.setType("password");

		// row = new Row();
		// row.setStyle("border:0px;background: transparent;");
		// row.setParent(rows);
		South south = new South();
		south.setFlex(true);
		south.setParent(borderlayout);

		Toolbar toolbar = new Toolbar();
		toolbar.setHeight("30px");
		toolbar.setParent(south);
		Toolbarbutton cancel = new Toolbarbutton("Batal", "/img/cancel.gif");
		cancel.setTooltiptext("Tutup");
		cancel.addEventListener("onClick", new EventListener() {
			@Override
			public void onEvent(Event event) throws Exception {
				ChangePasswordWindow.this.detach();
			}
		});
		// cancel.setParent(toolbar);

		Toolbarbutton print = new Toolbarbutton("Simpan", "/img/save.gif");
		print.addEventListener("onClick", new EventListener() {
			@Override
			public void onEvent(Event event) throws Exception {
				onChangePassword(event);
			}
		});
		print.setParent(toolbar);

	}

	@SuppressWarnings({})
	public void onChangePassword(Event event) throws Exception {

		Tbmuser users = (Tbmuser) Sessions.getCurrent().getAttribute("users");
		if (users == null) {
			users = (Tbmuser) Sessions.getCurrent().getAttribute("usersTemp");
		}

		System.out.println("users = " + users);

		if (passwordLama.getValue().trim().equals("")) {
			Messagebox.show(
					"Password lama yang anda masukkan tidak boleh kosong",
					"Peringatan", Messagebox.OK, Messagebox.EXCLAMATION);
			return;
		}
		if (passwordBaru.getValue().trim().equals("")) {
			Messagebox.show(
					"Password baru yang anda masukkan tidak boleh kosong",
					"Peringatan", Messagebox.OK, Messagebox.EXCLAMATION);
			return;
		}

		if (!passwordLama
				.getValue()
				.trim()
				.equals(Common.desEncrypter.decrypt(users.getUserPassword()
						.trim()))) {
			Messagebox.show("Password lama yang anda masukkan salah",
					"Peringatan", Messagebox.OK, Messagebox.EXCLAMATION);
			return;
		}

		if (!passwordBaru.getValue().trim()
				.equals(passwordBaruRepeat.getValue().trim())) {
			Messagebox.show("Password baru yang anda masukkan tidak sama",
					"Peringatan", Messagebox.OK, Messagebox.EXCLAMATION);
			return;
		}

		users.setUserPassword(Common.desEncrypter.encrypt((passwordBaru
				.getValue().trim())));
		Session ses = HibernateUtil.currentSession();

		ses.update(ses.merge(users));

		// session.setAttribute("users", users);
		Sessions.getCurrent().setAttribute("users", users);
		Messagebox.show("Perubahan password berhasil dilakukan", "Peringatan",
				Messagebox.OK, Messagebox.EXCLAMATION);
		passwordLama.setText("");
		passwordBaru.setText("");
		passwordBaruRepeat.setText("");
		// changePassword.setVisible(false);

	}

}
